/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlleur;

import gui.Grille;
import java.util.ArrayList;
import jreversi.Coordonnee;
import manager.EventManager;
import moteur.Plateau;

/**
 *
 * @author Jean
 */
public class ControllerGrille
{

    private Plateau plateau;
    ArrayList<Coordonnee> casesAFlip = new ArrayList<Coordonnee>();
    ArrayList<Coordonnee> casesTemp = new ArrayList<Coordonnee>();

    public ControllerGrille()
    {
        plateau = Plateau.getInstance();
    }

    public ControllerGrille(Plateau plateau)
    {
        this.plateau = plateau;
    }

    public boolean coupPossible(int x, int y)
    {
        casesAFlip.clear();
        casesTemp.clear();

        boolean res = false;

        if (plateau.getTabPlateau()[x][y] == 'e')
        {
            int borneXinf = - 1;
            int borneXsup = +1;
            int borneYinf = - 1;
            int borneYsup = +1;


            if (x + borneXinf < 0)
            {
                borneXinf = 0;
            }
            if (y + borneYinf < 0)
            {
                borneYinf = 0;
            }
            if (x + borneXsup >= plateau.getTabPlateau().length)
            {
                borneXsup = 0;
            }
            if (y + borneYsup >= plateau.getTabPlateau()[0].length)
            {
                borneYsup = 0;
            }

            for (int i = borneXinf; i <= borneXsup; i++)
            {
                for (int j = borneYinf; j <= borneYsup; j++)
                {
                    if (plateau.getTabPlateau()[x + i][y + j] != plateau.tour
                            && plateau.getTabPlateau()[x + i][y + j] != 'e')
                    {
                        if (pionAReturn(x, y, i, j))
                        {
                            casesAFlip.addAll(casesTemp);
                        }
                    }

                }
            }

            if (casesAFlip.size() > 0)
            {
                res = true;
            }
        }

        return res;
    }

    private boolean pionAReturn(int x, int y, int decalageX, int decalageY)
    {
        boolean continuer = true;
        boolean res = false;
        boolean inversPionDetect = false;
        int decalX = decalageX;
        int decalY = decalageY;

        while (continuer)
        {
            int xPlusDecalX = x + decalX;
            int yPlusDecalY = y + decalY;

            if ((plateau.getTabPlateau().length > xPlusDecalX && xPlusDecalX >= 0)
                    && (plateau.getTabPlateau()[0].length > yPlusDecalY && yPlusDecalY >= 0))
            {
                if (plateau.getTabPlateau()[xPlusDecalX][yPlusDecalY] == 'e')
                {
                    continuer = false;
                }
                else if (plateau.getTabPlateau()[xPlusDecalX][yPlusDecalY] == plateau.tour)
                {
                    //si un pion inverse de notre couleur a été trouver sur le chemin alors c'est bueno
                    if (inversPionDetect)
                    {
                        continuer = false;
                        res = true;
                    }
                    else
                    {
                        continuer = false;
                    }
                }
                else // si c'est pas notre couleur et que c'est pas vide alors c'est l autre
                {
                    casesTemp.add(new Coordonnee(xPlusDecalX, yPlusDecalY));
                    inversPionDetect = true;
                    decalX += decalageX;
                    decalY += decalageY;
                }
            }
            else
            {
                continuer = false;
            }
        }
        if (!res)
        {
            casesTemp.clear();
        }

        return res;
    }

    public ArrayList<Coordonnee> getCasesAFlip()
    {
        return casesAFlip;
    }

    public ArrayList<Coordonnee> rechercheCoups()
    {

        ArrayList<Coordonnee> cases = new ArrayList();
        char[][] tableau = plateau.getTabPlateau();

        for (int i = 0; i < tableau.length; i++)
        {
            for (int j = 0; j < tableau[0].length; j++)
            {
                if (coupPossible(i, j))
                {
                    cases.add(new Coordonnee(i, j));
                }
            }
        }

        return cases;
    }

    public boolean jeuFini()
    {
        boolean res = true;
        boolean boucler = true;
        int i = 0;
        int j = 0;

        char tab[][] = plateau.getTabPlateau();
        while (boucler)
        {
            if (tab[i][j] == 'e')
            {
                boucler = false;
                res = false;
            }
            else
            {
                j++;
                if (j >= tab.length)
                {
                    j = 0;
                    i++;
                }
                if (i >= tab.length)
                {
                    boucler = false;
                }
            }

        }
        return res;
    }

    public void newPartie()
    {
        Plateau.reset();
        EventManager.reset();
        Grille.reset();
    }
}
